﻿using System;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

using Jungle.Code.DAL;
using Jungle.Code.Utils;

namespace Jungle.Rent
{
    public class Offer
    {
        #region Fields
        private int offerId;             public int OfferId { get { return offerId; } set { offerId = value; } }
        private int objectTypeId;        public int ObjectTypeId { get { return objectTypeId; } set { objectTypeId = value; } }
        private int countryId;           public int CountryId { get { return countryId; } set { countryId = value; } }
        private string city;             public string City { get { return city; } set { city = value; } }
        private string adress;           public string Adress { get { return adress; } set { adress = value; } }
        private string description;      public string Description { get { return description; } set { description = value; } }
        private int rooms;               public int Rooms { get { return rooms; } set { rooms = value; } }
        private double groundTax=0;        public double GroundTax { get { return groundTax; } set { groundTax = value; } }
        private double price = 0; public double Price { get { return price; } set { price = value; } }
        private double tax = 0; public double Tax { get { return tax; } set { tax = value; } }
        private int taxPeriod;           public int TaxPeriod { get { return taxPeriod; } set { taxPeriod = value; } }
        private int floor;               public int Floor { get { return floor; } set { floor = value; } }
        private int floors;              public int Floors { get { return floors; } set { floors = value; } }
        private string photos;           public string Photos { get { return photos; } set { photos = value; } }
        private double squareTotal = 0; public double SquareTotal { get { return squareTotal; } set { squareTotal = value; } }
        private double squareLive = 0; public double SquareLive { get { return squareLive; } set { squareLive = value; } }
        private double squareKit = 0; public double SquareKit { get { return squareKit; } set { squareKit = value; } }
        private double areaSquare = 0; public double AreaSquare { get { return areaSquare; } set { areaSquare = value; } }
        private int balcon;              public int Balcon { get { return balcon; } set { balcon = value; } }
        private int bathroom;            public int Bathroom { get { return bathroom; } set { bathroom = value; } }
        private int bedroom;             public int Bedroom { get { return bedroom; } set { bedroom = value; } }
        private int kitchen;             public int Kitchen { get { return kitchen; } set { kitchen = value; } }
        private string toSea;            public string ToSea { get { return toSea; } set { toSea = value; } }
        private bool laundry;            public bool Laundry { get { return laundry; } set { laundry = value; } }
        private bool storage;            public bool Storage { get { return storage; } set { storage = value; } }
        private bool condition;          public bool Condition { get { return condition; } set { condition = value; } }
        private bool farniture;          public bool Farniture { get { return farniture; } set { farniture = value; } }
        private bool heating;            public bool Heating { get { return heating; } set { heating = value; } }
        private bool parking;            public bool Parking { get { return parking; } set { parking = value; } }
        private bool seaPanarama;        public bool SeaPanarama { get { return seaPanarama; } set { seaPanarama = value; } }
        private bool garden;            public bool Garden { get { return garden; } set { garden = value; } }
        private bool barbeku;            public bool Barbeku { get { return barbeku; } set { barbeku = value; } }
        private bool pool;               public bool Pool { get { return pool; } set { pool = value; } }
        private bool sallon;             public bool Sallon { get { return sallon; } set { sallon = value; } }
        private string contacts;         public string Contacts { get { return contacts; } set { contacts = value; } }
        private DateTime pubDate;        public DateTime PubDate { get { return pubDate; } set { pubDate = value; } }
        private bool activation;         public bool Activation { get { return activation; } set { activation = value; } }
        #endregion

        public Offer() { }
        // ---------------------------------------------------------------------------------
        public Offer(int _OfferId)
        {
            offerId = _OfferId;
            GetById();
        }
        // ---------------------------------------------------------------------------------
        public Offer(SqlDataReader reader)
        {
            ReadReader(reader);
        }
        // ---------------------------------------------------------------------------------
        public string err;
        public List<Offer> OfferList = new List<Offer>();
        // ---------------------------------------------------------------------------------
        public void Add()
        {
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("rent.Offer_Add", connect);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@ObjectTypeId", SqlDbType.Int).Value = ObjectTypeId;
                    command.Parameters.Add("@CountryId", SqlDbType.Int).Value = CountryId;
                    command.Parameters.Add("@CityId", SqlDbType.VarChar, 64).Value = city;
                    command.Parameters.Add("@Adress", SqlDbType.VarChar, 128).Value = Adress;
                    command.Parameters.Add("@Description", SqlDbType.VarChar).Value = Description;
                    command.Parameters.Add("@Rooms", SqlDbType.Int).Value = Rooms;
                    command.Parameters.Add("@GroundTax", SqlDbType.Float).Value = GroundTax;
                    command.Parameters.Add("@Price", SqlDbType.Float).Value = Price;
                    command.Parameters.Add("@Tax", SqlDbType.Float).Value = Tax;
                    command.Parameters.Add("@TaxPeriod", SqlDbType.Float).Value = TaxPeriod;
                    command.Parameters.Add("@Floor", SqlDbType.Int).Value = Floor;
                    command.Parameters.Add("@Floors", SqlDbType.Int).Value = Floors;
                    command.Parameters.Add("@Photos", SqlDbType.VarChar).Value = Photos;
                    command.Parameters.Add("@SquareTotal", SqlDbType.Float).Value = SquareTotal;
                    command.Parameters.Add("@SquareLive", SqlDbType.Float).Value = SquareLive;
                    command.Parameters.Add("@SquareKit", SqlDbType.Float).Value = SquareKit;
                    command.Parameters.Add("@AreaSquare", SqlDbType.Float).Value = AreaSquare;
                    command.Parameters.Add("@Balcon", SqlDbType.Int).Value = Balcon;
                    command.Parameters.Add("@Bathroom", SqlDbType.Int).Value = Bathroom;
                    command.Parameters.Add("@Bedroom", SqlDbType.Int).Value = Bedroom;
                    command.Parameters.Add("@Kitchen", SqlDbType.Int).Value = Kitchen;
                    command.Parameters.Add("@ToSea", SqlDbType.VarChar, 32).Value = ToSea;
                    command.Parameters.Add("@Laundry", SqlDbType.Bit).Value = Laundry;
                    command.Parameters.Add("@Storage", SqlDbType.Bit).Value = Storage;
                    command.Parameters.Add("@Condition", SqlDbType.Bit).Value = Condition;
                    command.Parameters.Add("@Farniture", SqlDbType.Bit).Value = Farniture;
                    command.Parameters.Add("@Heating", SqlDbType.Bit).Value = Heating;
                    command.Parameters.Add("@Parking", SqlDbType.Bit).Value = Parking;
                    command.Parameters.Add("@SeaPanarama", SqlDbType.Bit).Value = SeaPanarama;
                    command.Parameters.Add("@Garden", SqlDbType.Bit).Value = garden;
                    command.Parameters.Add("@Barbeku", SqlDbType.Bit).Value = Barbeku;
                    command.Parameters.Add("@Pool", SqlDbType.Bit).Value = Pool;
                    command.Parameters.Add("@Sallon", SqlDbType.Bit).Value = Sallon;
                    command.Parameters.Add("@Contacts", SqlDbType.VarChar).Value = Contacts;

                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message.ToString();
            }
        }
        // ---------------------------------------------------------------------------------
        public void Update()
        {
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("rent.Offer_Update", connect);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@OfferId", SqlDbType.Int).Value = offerId;
                    command.Parameters.Add("@ObjectTypeId", SqlDbType.Int).Value = ObjectTypeId;
                    command.Parameters.Add("@CountryId", SqlDbType.Int).Value = CountryId;
                    command.Parameters.Add("@CityId", SqlDbType.VarChar, 64).Value = city;
                    command.Parameters.Add("@Adress", SqlDbType.VarChar, 128).Value = Adress;
                    command.Parameters.Add("@Description", SqlDbType.VarChar).Value = Description;
                    command.Parameters.Add("@Rooms", SqlDbType.Int).Value = Rooms;
                    command.Parameters.Add("@GroundTax", SqlDbType.Float).Value = GroundTax;
                    command.Parameters.Add("@Price", SqlDbType.Float).Value = Price;
                    command.Parameters.Add("@Tax", SqlDbType.Float).Value = Tax;
                    command.Parameters.Add("@TaxPeriod", SqlDbType.Float).Value = TaxPeriod;
                    command.Parameters.Add("@Floor", SqlDbType.Int).Value = Floor;
                    command.Parameters.Add("@Floors", SqlDbType.Int).Value = Floors;
                    command.Parameters.Add("@Photos", SqlDbType.VarChar).Value = Photos;
                    command.Parameters.Add("@SquareTotal", SqlDbType.Float).Value = SquareTotal;
                    command.Parameters.Add("@SquareLive", SqlDbType.Float).Value = SquareLive;
                    command.Parameters.Add("@SquareKit", SqlDbType.Float).Value = SquareKit;
                    command.Parameters.Add("@AreaSquare", SqlDbType.Float).Value = AreaSquare;
                    command.Parameters.Add("@Balcon", SqlDbType.Int).Value = Balcon;
                    command.Parameters.Add("@Bathroom", SqlDbType.Int).Value = Bathroom;
                    command.Parameters.Add("@Bedroom", SqlDbType.Int).Value = Bedroom;
                    command.Parameters.Add("@Kitchen", SqlDbType.Int).Value = Kitchen;
                    command.Parameters.Add("@ToSea", SqlDbType.VarChar, 32).Value = ToSea;
                    command.Parameters.Add("@Laundry", SqlDbType.Bit).Value = Laundry;
                    command.Parameters.Add("@Storage", SqlDbType.Bit).Value = Storage;
                    command.Parameters.Add("@Condition", SqlDbType.Bit).Value = Condition;
                    command.Parameters.Add("@Farniture", SqlDbType.Bit).Value = Farniture;
                    command.Parameters.Add("@Heating", SqlDbType.Bit).Value = Heating;
                    command.Parameters.Add("@Parking", SqlDbType.Bit).Value = Parking;
                    command.Parameters.Add("@SeaPanarama", SqlDbType.Bit).Value = SeaPanarama;
                    command.Parameters.Add("@Garden", SqlDbType.Bit).Value = garden;
                    command.Parameters.Add("@Barbeku", SqlDbType.Bit).Value = Barbeku;
                    command.Parameters.Add("@Pool", SqlDbType.Bit).Value = Pool;
                    command.Parameters.Add("@Sallon", SqlDbType.Bit).Value = Sallon;
                    command.Parameters.Add("@Contacts", SqlDbType.VarChar).Value = Contacts;

                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message.ToString();
            }
        }
        // ---------------------------------------------------------------------------------
        public void GetById()
        {
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("select * from rent.Offer where [OfferId]=" + offerId.ToString(), connect);

                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        ReadReader(reader);
                    }
                    reader.Close();
                    command.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message.ToString();
            }
        }
        // ---------------------------------------------------------------------------------
        public void GetAll()
        {
            OfferList = new List<Offer>();
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("select * from rent.Offer", connect);

                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        OfferList.Add(new Offer(reader));
                    }
                    reader.Close();
                    command.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message.ToString();
            }
        }
        private void ReadReader(SqlDataReader reader)
        {
            offerId = (int)reader["OfferId"];
            ObjectTypeId = (int)reader["ObjectTypeId"];
            CountryId = (int)reader["CountryId"];
            city = (string)reader["CityId"];
            adress = (string)reader["Adress"];
            description = (string)reader["Description"];
            rooms = (int)reader["Rooms"];
            groundTax = (double)reader["GroundTax"];
            price = (double)reader["Price"];
            tax = (double)reader["Tax"];
            taxPeriod = (int)reader["TaxPeriod"];
            floor = (int)reader["Floor"];
            floors = (int)reader["Floors"];
            photos = (string)reader["Photos"];
            squareTotal = (double)reader["SquareTotal"];
            squareLive = (double)reader["SquareLive"];
            squareKit = (double)reader["SquareKit"];
            areaSquare = (double)reader["AreaSquare"];
            balcon = (int)reader["Balcon"];
            bathroom = (int)reader["Bathroom"];
            bedroom = (int)reader["Bedroom"];
            kitchen = (int)reader["Kitchen"];
            toSea = (string)reader["ToSea"];
            laundry = (bool)reader["Laundry"];
            storage = (bool)reader["Storage"];
            condition = (bool)reader["Condition"];
            farniture = (bool)reader["Farniture"];
            heating = (bool)reader["Heating"];
            parking = (bool)reader["Parking"];
            seaPanarama = (bool)reader["SeaPanarama"];
            garden = (bool)reader["Garden"];
            barbeku = (bool)reader["Barbeku"];
            pool = (bool)reader["Pool"];
            sallon = (bool)reader["Sallon"];
            contacts = (string)reader["Contacts"];
            pubDate = (DateTime)reader["PubDate"];
            activation = (bool)reader["Activation"];            
        }
        // ---------------------------------------------------------------------------------
        public void Delete()
        {
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("delete from rent.Offer where [OfferId]=" + offerId.ToString(), connect);

                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message.ToString();
            }
        }
        // ---------------------------------------------------------------------------------
        public static void Delete(int OfferId)
        {
            Offer o = new Offer(); o.OfferId = OfferId;
            o.Delete();
        }
        // ---------------------------------------------------------------------------------
        public static DropDownList ddl_ObjectType()
        {
            DropDownList ddl = new DropDownList();
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("select * from rent.ObjectType", connect);
                    SqlDataReader reader = null;

                    command.Connection.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        ddl.Items.Add(new ListItem(reader["Value"].ToString().Trim(), reader["ObjectTypeId"].ToString()));
                    }
                    reader.Close();
                    command.Connection.Close();
                }
            }
            catch { }

            return ddl;
        }
        // ---------------------------------------------------------------------------------
        public static DropDownList ddl_Countrys()
        {
            DropDownList ddl = new DropDownList();
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("select * from rent.Countrys", connect);
                    SqlDataReader reader = null;

                    command.Connection.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        ddl.Items.Add(new ListItem(reader["Value"].ToString().Trim(), reader["CountryId"].ToString()));
                    }
                    reader.Close();
                    command.Connection.Close();
                }
            }
            catch { }

            return ddl;
        }
        // ---------------------------------------------------------------------------------
        public static List<Offer> SearchResult(int CountryId, int RegionId, int ObjectTypeId, double Square1, double Square2, double Price1, double Price2)
        {
            List<Offer> OfferList = new List<Offer>();
            try
            {
                using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
                {
                    SqlCommand command = new SqlCommand("rent.Offer_Search", connect);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@Country", SqlDbType.Int).Value = CountryId;
                    command.Parameters.Add("@Region", SqlDbType.Int).Value = RegionId;
                    command.Parameters.Add("@ObjectType", SqlDbType.Int).Value = ObjectTypeId;
                    command.Parameters.Add("@Square1", SqlDbType.Float).Value = Square1;
                    command.Parameters.Add("@Square2", SqlDbType.Float).Value = Square2;
                    command.Parameters.Add("@Price1", SqlDbType.Float).Value = Price1;
                    command.Parameters.Add("@Price2", SqlDbType.Float).Value = Price2;


                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        OfferList.Add(new Offer(reader));
                    }
                    reader.Close();
                    command.Connection.Close();
                }
            }
            catch
            {
            }

            return OfferList;
        }
    }
}